﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.App_Code
{
    using ECommon.Components;
    using ECommon.Utilities;
    using ENode.Commanding;
    using ENode.Configurations;
    using ENode.EQueue;
    using ENode.EQueue.Commanding;
    using ENode.Infrastructure;
    using ENode.Infrastructure.Impl;
    using EQueue.Clients.Producers;
    using EQueue.Configurations;
    using Infrastructure;

    using Commands.Accounts;
    //using Domain.Accounts;
    //using Messages;
    //using ProcessManagers;
    //using Denormalizers;
    using System.Net;
    using ECommon.Socketing;

    public static class ENodeExtensions
    {
        private static CommandService _commandService;

        public static ENodeConfiguration RegisterAllTypeCodes(ENodeConfiguration enodeConfiguration)
        {
            var provider = ObjectContainer.Resolve<ITypeCodeProvider>() as DefaultTypeCodeProvider;

            //commands
            provider.RegisterType<RegisterNewAccountCommand>(AllTypeCodes.RegisterNewAccountCommand);
            provider.RegisterType<SignInAfterCommand>(AllTypeCodes.SignInAfterCommand);
            provider.RegisterType<SignOutAfterCommand>(AllTypeCodes.SignOutAfterCommand);



            provider.RegisterType<Commands.Sections.CreateSectionCommand>(AllTypeCodes.CreateSectionCommand);
            provider.RegisterType<Commands.Sections.ChangeSectionNameCommand>(AllTypeCodes.ChangeSectionNameCommand);

            provider.RegisterType<Commands.Posts.CreatePostCommand>(AllTypeCodes.CreatePostCommand);
            provider.RegisterType<Commands.Posts.UpdatePostCommand>(AllTypeCodes.UpdatePostCommand);
            provider.RegisterType<Commands.Posts.AcceptNewReplyCommand>(AllTypeCodes.AcceptNewReplyCommand);

            provider.RegisterType<Commands.Replies.CreateReplyCommand>(AllTypeCodes.CreateReplyCommand);
            provider.RegisterType<Commands.Replies.ChangeReplyBodyCommand>(AllTypeCodes.ChangeReplyBodyCommand);


            return enodeConfiguration;
        }
        public static ENodeConfiguration UseEQueue(ENodeConfiguration enodeConfiguration)
        {
            var configuration = enodeConfiguration.GetCommonConfiguration();

            configuration.RegisterEQueueComponents();

            _commandService = new CommandService(
                new CommandResultProcessor(new IPEndPoint(SocketUtils.GetLocalIPV4(), 9001)),//使用这个去连接eq？
                "CommandService",
                new ProducerSetting { BrokerAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), ConfigSettings.BrokerProducerPort) });

            configuration.SetDefault<ICommandService, CommandService>(_commandService);

            return enodeConfiguration;
        }

        public static ENodeConfiguration Start(ENodeConfiguration enodeConfiguration)
        {
            _commandService.Start();
            return enodeConfiguration;
        }

    }
}